package com.sccba.emtbusiness.service.impl;

import java.util.List;

import com.sccba.common.exception.ServiceException;
import com.sccba.common.utils.DateUtils;
import com.sccba.common.utils.SecurityUtils;
import com.sccba.emtbusiness.domain.YjczProductEventInfo;
import com.sccba.emtbusiness.enums.ProductEventStatus;
import com.sccba.emtbusiness.service.IYjczProductEventInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sccba.emtbusiness.mapper.YjczProductEventDetailMapper;
import com.sccba.emtbusiness.domain.YjczProductEventDetail;
import com.sccba.emtbusiness.service.IYjczProductEventDetailService;

/**
 * 生产事件补录Service业务层处理
 * 
 * @author lichh
 * @date 2024-04-25
 */
@Service
public class YjczProductEventDetailServiceImpl implements IYjczProductEventDetailService 
{
    @Autowired
    private YjczProductEventDetailMapper yjczProductEventDetailMapper;

    @Autowired
    private IYjczProductEventInfoService yjczProductEventInfoService;

    /**
     * 查询生产事件补录
     * 
     * @param pkId 生产事件补录主键
     * @return 生产事件补录
     */
    @Override
    public YjczProductEventDetail selectYjczProductEventDetailByPkId(Long pkId)
    {
        return yjczProductEventDetailMapper.selectYjczProductEventDetailByPkId(pkId);
    }

    @Override
    public YjczProductEventDetail selectDetailByEventId(Long eventId) {
        YjczProductEventDetail detail = new YjczProductEventDetail();
        detail.setDetailEventId(eventId);
        List<YjczProductEventDetail> details = yjczProductEventDetailMapper.selectYjczProductEventDetailList(detail);
        if (null == details || details.isEmpty()) {
            return null;
        }
        return details.get(0);
    }

    /**
     * 查询生产事件补录列表
     * 
     * @param yjczProductEventDetail 生产事件补录
     * @return 生产事件补录
     */
    @Override
    public List<YjczProductEventDetail> selectYjczProductEventDetailList(YjczProductEventDetail yjczProductEventDetail)
    {
        return yjczProductEventDetailMapper.selectYjczProductEventDetailList(yjczProductEventDetail);
    }

    /**
     * 新增生产事件补录
     * 
     * @param yjczProductEventDetail 生产事件补录
     * @return 结果
     */
    @Override
    public int insertYjczProductEventDetail(YjczProductEventDetail yjczProductEventDetail)
    {
        yjczProductEventDetail.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        yjczProductEventDetail.setCreateTime(DateUtils.getNowDate());
        return yjczProductEventDetailMapper.insertYjczProductEventDetail(yjczProductEventDetail);
    }

    /**
     * 修改生产事件补录
     * 
     * @param yjczProductEventDetail 生产事件补录
     * @return 结果
     */
    @Override
    public int updateYjczProductEventDetail(YjczProductEventDetail yjczProductEventDetail)
    {
        yjczProductEventDetail.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        yjczProductEventDetail.setUpdateTime(DateUtils.getNowDate());
        return yjczProductEventDetailMapper.updateYjczProductEventDetail(yjczProductEventDetail);
    }

    /**
     * 批量删除生产事件补录
     * 
     * @param pkIds 需要删除的生产事件补录主键
     * @return 结果
     */
    @Override
    public int deleteYjczProductEventDetailByPkIds(Long[] pkIds)
    {
        return yjczProductEventDetailMapper.deleteYjczProductEventDetailByPkIds(pkIds);
    }

    /**
     * 删除生产事件补录信息
     * 
     * @param pkId 生产事件补录主键
     * @return 结果
     */
    @Override
    public int deleteYjczProductEventDetailByPkId(Long pkId)
    {
        return yjczProductEventDetailMapper.deleteYjczProductEventDetailByPkId(pkId);
    }

    @Override
    public void submitProductEventDetail(YjczProductEventDetail eventDetail) {
        Long pkId = eventDetail.getPkId();
        if (null == pkId) {
            Long eventId = eventDetail.getDetailEventId();
            YjczProductEventInfo eventInfo = yjczProductEventInfoService.selectYjczProductEventInfoByPkId(eventId);
            if (null == eventInfo) {
                throw new ServiceException("未查询到相应的生产事件");
            }
            insertYjczProductEventDetail(eventDetail);
            Integer eventState = eventInfo.getEventState();
            if (eventState == ProductEventStatus.WAIT_FILL.getCode()) {
                yjczProductEventInfoService.updateEventState(ProductEventStatus.DONE.getCode(), eventId);
            }
        } else {
            updateYjczProductEventDetail(eventDetail);
        }
    }
}
